package com.gin.supermarket_inventory_system.commodity.dto.vo;

import com.gin.common.utils.TreeNode;
import com.gin.database.base.BaseVo;
import com.gin.supermarket_inventory_system.commodity.entity.CommodityTypePo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品类型
 * @since : 2023/10/9 15:15
 * @author : ginstone
 * @version : v1.0.0
 **/
@Getter
@Setter
@Schema(description = "视图对象:商品类型")
public class CommodityTypeVo extends BaseVo implements TreeNode<CommodityTypeVo> {
    @Schema(description = "名称")
    String name;
    @Schema(description = "父类型Id")
    Long parentId;

    @Schema(description = "子类型")
    List<CommodityTypeVo> children;

    public CommodityTypeVo(CommodityTypePo po) {
        // 复制Po的字段
        BeanUtils.copyProperties(po, this);
    }

    /**
     * 将持久对象列表转换为视图对象列表
     * @param data 持久对象列表
     * @return 视图对象列表
     */
    public static List<CommodityTypeVo> toVoList(List<CommodityTypePo> data) {
        if (data == null) {
            return new ArrayList<>();
        }
        return data.stream().map(CommodityTypeVo::new).toList();
    }

    @Override
    public Serializable getNodeId() {
        return getId();
    }

    @Override
    public Serializable getParentNodeId() {
        return parentId;
    }

    @Override
    public List<CommodityTypeVo> getChildrenNodeList() {
        return children;
    }

    @Override
    public void setChildrenNodeList(List<CommodityTypeVo> list) {
        children = list;
    }
}
